# coding:utf-8
from app.constant import HttpCode


class Code(object):
    def __init__(self, code, msg='', http_code=HttpCode.SUCCESS):
        self.code = code
        self.msg = msg
        self.http_code = http_code

    def __str__(self):
        return "({code}, {msg})".format(code=self.code, msg=self.msg)

    def __repr__(self):
        return "({code}, {msg})".format(code=self.code, msg=self.msg)

    def new_format_msg(self, *args):
        """
        add by ld_wu
        用于需要格式化消息
        :param args:
        :return:
        """
        # 主要为了解决实例对象共用冲突问题
        return Code(self.code, self.msg % args, self.http_code)

    def json(self):
        return {
            'code': self.code,
            'message': self.msg,
            'body': None
        }

# api调用成功
SUCCESS = Code('000000')

# 缺少必要参数
PARAMETER_REQUIRED = Code('COM001', '缺少请求参数%s', HttpCode.BAD_REQUEST)